<?php
namespace app\api\controller\works;
use app\common\controller\Api;
use think\Db;
class Catalog extends Api{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected function _initialize()
    {
        parent::_initialize();

    }
    public function orderIdGetCatalog(){
        $userid = $this->auth->id;
        $id = $this->request->param('id');
        $works = Db::name('WorksCatalog')->where('user_id',$userid)->where('order_id',$id)->find();
        $this->success('作品ID',['id'=> $works['id']]);
    }
    public function choose(){
        $userid = $this->auth->id;
        $id = $this->request->param('id');
        $works = Db::name('WorksCatalog')->where('user_id',$userid)->where('id',$id)->find();
        $goods = Db::name('Formwork')->where('category_id',$works['category_id'])->field('id,title,image,photo_min,photo_max')->select();
        if(!empty($goods)){
            foreach ($goods as $key => &$value) {
                $value['image'] = cdnurl($value['image'])."?x-oss-process=image/resize,w_750";
            }
        }
        $this->success('作品模板', $goods);
    }
    public function index(){
        $userid =$this->auth->id;
        $page = $this->request->param('page',1);
        $pagesize = $this->request->param('pagesize',10);
        $works = Db::name('WorksCatalog')->where('user_id',$userid)->order('id desc')->page($page,$pagesize)->select();
        if(!empty($works)){
            foreach ($works as $key => &$value) {
                $value['image'] = cdnurl($value['image'])."?x-oss-process=image/resize,w_750";
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $value['goods_title'] = Db::name('ShoproGoods')->where('id',$value['goods_id'])->value('title');
                $value['formwork_title'] = Db::name('Formwork')->where('id',$value['formwork_id'])->value('title');
            }
        }else{
            $works =[];
            
        }
        $data['page'] = $page;
        $data['pagesize']=$pagesize;
        $data['list'] = $works;
        $this->success('作品列表', $data);
    }
    public function edit(){
        $userid = $this->auth->id;
        $id = $this->request->param('id');
        $title = $this->request->param('title');
        $author = $this->request->param('author');

        Db::name('WorksCatalog')->where('id',$id)->update(['title'=>$title,'author'=>$author]);
        $this->success('作品修改成功');
    }
    //作品图片对象数组[{'image':'\xiangce\xess3432.jpg','weigh':'1'},{'image':'\xiangce\xess3432.jpg','weigh':'2'},{'image':'\xiangce\xess3432.jpg','weigh':'3'}]
    public function finish(){
        $userid = $this->auth->id;
        $images = $this->request->post('images/a');
        $id = $this->request->post('id',0);
        if($id<1){
            $this->error('作品ID为空');
        }
        
        if(empty($images)){
            $this->error('作品没有图片列表');
        }
        Db::startTrans();
        try {
            $works = Db::name('WorksCatalog')->where('id',$id)->find();
            Db::name('WorksPhoto')->where('works_catalog_id',$works['id'])->delete();
            $photo = Db::name('AlbumPhoto')->where('id','in',array_column($images,'id'))->column('id,image');
            $workphoto = [];
            foreach ($images as $key => $value) {
                $workphoto[$key]['works_catalog_id'] = $works['id'];
                $workphoto[$key]['image'] = $photo[$value['id']];
                $workphoto[$key]['weigh'] = $value['weigh'];
                $workphoto[$key]['createtime'] = time();
                $workphoto[$key]['updatetime'] = time();
            }

            Db::name('WorksPhoto')->insertAll($workphoto);
            Db::name('WorksCatalog')->where('id',$id)->update(['is_finish'=>1]);
            Db::commit();
        } catch (\think\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('作品完成');
        
    }
    public function detail(){
        $userid = $this->auth->id;
        $id = $this->request->param('id');
        $works = Db::name('WorksCatalog')->where('user_id',$userid)->where('id',$id)->find();
        if($works){
            $works['image'] = cdnurl($works['image'])."?x-oss-process=image/resize,w_750";
            $works['createtime'] = date('Y-m-d H:i:s',$works['createtime']);
            $works['goods_title'] = Db::name('ShoproGoods')->where('id',$works['goods_id'])->value('title');
            $works['formwork_title'] = Db::name('Formwork')->where('id',$works['formwork_id'])->value('title');
            $images = Db::name('WorksPhoto')->where('works_catalog_id',$works['id'])->order('weigh asc')->field('image,weigh')->select();
            if(!empty($images)){
            foreach($images as $k =>&$v){
                $v['image'] = cdnurl($v['image'])."?x-oss-process=image/resize,w_750";
            }
            $works['images'] = $images;
            }else{
                $works['images'] =[];
            }
        }
        $this->success('作品', $works);
    }
    public function qure(){
        $userid = $this->auth->id;
        $id = $this->request->param('id');
        $goods_id = $this->request->param('formwork_id');
        $goods = Db::name('Formwork')->where('id',$goods_id)->field('title,image,photo_min,photo_max')->find();
        Db::name('WorksCatalog')->where('id',$id)->update(['photo_min'=>$goods['photo_min'],'photo_max'=>$goods['photo_max'],'image'=>$goods['image']]);
        $works = Db::name('WorksCatalog')->where('id',$id)->find();
        $this->success('切换成功');
    }

    public function formwork(){
        $userid = $this->auth->id;
        $id = $this->request->param('id');
        $works = Db::name('WorksCatalog')->where('user_id',$userid)->where('id',$id)->find();
        if($works){
            $works['image'] = cdnurl($works['image'])."?x-oss-process=image/resize,w_750";
            $works['createtime'] = date('Y-m-d H:i:s',$works['createtime']);
            $works['goods_title'] = Db::name('ShoproGoods')->where('id',$works['goods_id'])->value('title');
            $works['formwork_title'] = Db::name('Formwork')->where('id',$works['formwork_id'])->value('title');
            $images = Db::name('WorksFormwork')->where('works_catalog_id',$works['id'])->order('weigh asc')->field('id,image,weigh')->select();
            if(!empty($images)){
            foreach($images as $k =>&$v){
                $v['image'] = cdnurl($v['image'])."?x-oss-process=image/resize,w_750";
            }
            $works['formwork'] = $images;
            }else{
                $works['images'] =[];
            }
        }
        $this->success('作品相册', $works);
    }
}

?>